package net.cassite.daf4j.jpa;

import net.cassite.daf4j.*;
import net.cassite.daf4j.ds.AndOrParser;
import net.cassite.daf4j.ds.ObjectResolver;

import java.util.List;

/**
 * 将条件转化为JPQL表达式
 */
public class JPQLAndOrParser implements AndOrParser<JPQLContext, String> {
        private ObjectResolver<JPQLContext, String> objectResolver;

        @Override
        public String and(JPQLContext jpqlContext, List<Condition> list, List<ExpressionBoolean> list1) throws Exception {
                StringBuilder sb = new StringBuilder();
                boolean isFirst = true;
                for (Condition c : list) {
                        if (jpqlContext.GeneratingWhere && DataUtils.getAggregate(c) != null) continue;
                        if (isFirst) {
                                isFirst = false;
                        } else {
                                sb.append(" AND ");
                        }
                        sb.append(objectResolver.resolve(jpqlContext, c));
                }
                for (ExpressionBoolean exp : list1) {
                        if (jpqlContext.GeneratingWhere && DataUtils.getAggregate(exp) != null) continue;
                        if (isFirst) {
                                isFirst = false;
                        } else {
                                sb.append(" AND ");
                        }
                        sb.append(objectResolver.resolve(jpqlContext, exp));
                }
                String res = sb.toString().trim();
                if (res.length() == 0) return null;
                return res;
        }

        @Override
        public String or(JPQLContext jpqlContext, List<Condition> list, List<ExpressionBoolean> list1) throws Exception {
                StringBuilder sb = new StringBuilder();
                boolean isFirst = true;
                for (Condition c : list) {
                        if (isFirst) {
                                isFirst = false;
                        } else {
                                sb.append(" OR ");
                        }
                        sb.append(objectResolver.resolve(jpqlContext, c));
                }
                for (ExpressionBoolean exp : list1) {
                        if (isFirst) {
                                isFirst = false;
                        } else {
                                sb.append(" OR ");
                        }
                        sb.append(objectResolver.resolve(jpqlContext, exp));
                }
                String res = sb.toString().trim();
                if (res.length() == 0) return null;
                return res;
        }

        @Override
        public String and(JPQLContext jpqlContext, List<String> list) throws Exception {
                StringBuilder sb = new StringBuilder();
                boolean isFirst = true;
                for (String str : list) {
                        if (isFirst) {
                                isFirst = false;
                        } else {
                                sb.append(" AND ");
                        }
                        sb.append("(").append(str).append(")");
                }
                String res = sb.toString().trim();
                if (res.length() == 0) return null;
                return res;
        }

        @Override
        public String or(JPQLContext jpqlContext, List<String> list) throws Exception {
                StringBuilder sb = new StringBuilder();
                boolean isFirst = true;
                for (String str : list) {
                        if (isFirst) {
                                isFirst = false;
                        } else {
                                sb.append(" OR ");
                        }
                        sb.append("(").append(str).append(")");
                }
                String res = sb.toString().trim();
                if (res.length() == 0) return null;
                return res;
        }

        @Override
        public JPQLContext beforeParsingAnd(JPQLContext jpqlContext, And and) throws Exception {
                return jpqlContext;
        }

        @Override
        public String afterParsingAnd(JPQLContext jpqlContext, String s) throws Exception {
                return s;
        }

        @Override
        public JPQLContext beforeParsingOr(JPQLContext jpqlContext, Or or) throws Exception {
                return jpqlContext;
        }

        @Override
        public String afterParsingOr(JPQLContext jpqlContext, String s) throws Exception {
                return s;
        }

        @Override
        public void setObjectResolver(ObjectResolver<JPQLContext, String> objectResolver) {
                this.objectResolver = objectResolver;
        }
}
